package com.gdth.bank.model;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TAppFjxxbDao;
import com.gdth.http.method.HttpMethodServlet;

import org.greenrobot.greendao.query.QueryBuilder;


public class CjkhFjxxModel {
	/**
	 * 查询当前采访客户的所有附件
	 * @param val
	 * @return
	 */
	public Observable<List<TAppFjxxb>> getFjxxByUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppFjxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppFjxxb>> arg0) {
				// TODO 查询当前采访客户的所有附件
				TAppFjxxbDao dao = BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> qb = dao.queryBuilder();

			if(!jcuuid.equals("")){
				qb.where(TAppFjxxbDao.Properties.Wybs.like(jcuuid)).orderDesc(TAppFjxxbDao.Properties.Id);
			}else{
				// 2019 -6-13 xjc
				//qb.where(TAppFjxxbDao.Properties.Sczt.eq("3"));
				qb.whereOr(TAppFjxxbDao.Properties.Sczt.eq("3"),TAppFjxxbDao.Properties.Sczt.eq("0"));
			
			}
				List<TAppFjxxb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
			
		});
	}
	
	/**
	 * 采访客户附件保存
	 * @param mTAppFjxxb
	 * @return
	 */
	public Observable<Long> saveCjkhFjxx(TAppFjxxb mTAppFjxxb){
		final TAppFjxxb tAppFjxxb = mTAppFjxxb;
		return Observable.create(new Observable.OnSubscribe<Long>() {

			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO 采访客户附件保存
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppFjxxbDao dao = session.getTAppFjxxbDao();
						
						try {
							dao.insertInTx(tAppFjxxb);
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							int i=tAppFjxxb.getZdlx();
							arg0.onNext(tAppFjxxb.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
	
	
	public Observable<List<TAppFjxxb>> getFjxxuuid(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppFjxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppFjxxb>> arg0) {
				// TODO 查询当前采访客户的所有附件
				TAppFjxxbDao dao = BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> qb = dao.queryBuilder();
				if(!uuid.equals("")){
					qb.where(TAppFjxxbDao.Properties.Uuid.eq(uuid));
				}else{
					// 2019 -6-13 xjc
					qb.where(TAppFjxxbDao.Properties.Gnmk.eq("1"),TAppFjxxbDao.Properties.Sczt.eq("0"));
				}
			
				List<TAppFjxxb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
			
		});
	}
	//判断网络
		public Observable<String> getWl(String content){
			return HttpMethodServlet.getInstance().getWl(content);
		}
}
